home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MemoryHe.h
-
- Contains: MemoryHeap class interface
-
- Owned by: Michael Burbidge, Jens Alfke
- Owned by: Jens Alfke
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <11> 10/24/95 jpa 1293441: Added slush-fund.
- <10> 8/4/95 DM Leak checking [1267956]
- <9> 5/4/95 jpa Support for finding largest free block
- [1235657] and validating memory ranges
- [1246077]
- <8> 10/24/94 jpa Constness [1194286].
- <7> 9/29/94 RA 1189812: Mods for 68K build.
- <6> 9/14/94 jpa Eliminated dependencies on rest of OpenDoc.
- Added ability to get heap of a block.
- [1186692]
- <5> 8/17/94 jpa Added support for walking heaps [1179567].
- <4> 8/8/94 jpa Added oldBlk param to DidRealloc hook
- [1179567]
- <3> 6/28/94 jpa Added no-op new/delete to MemoryHookList to
- avoid refs to ODDisposePtr.
- <2> 6/13/94 MB Some more initial fixes
- <2> 6/10/94 MB Make it build
- <1> 6/9/94 MB first checked in
- <3> 5/26/94 MB #1162181: Fixed MMM integration bug
- <2> 5/9/94 MB #1162181: Changes necessary to install MMM.
- <1> 4/29/94 MB first checked in
- To Do:
- In Progress:
-
- */
-
- #ifndef _MEMORYHE_
- #define _MEMORYHE_
-
- #ifndef _PLATFMEM_
- #include "PlatfMem.h"
- #endif
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
-
- //========================================================================================
- // Forward class declarations
- //========================================================================================
-
- class MemoryHookList;
-
-
- //========================================================================================
- // Type definitions
- //========================================================================================
-
- typedef unsigned long ODBytePtr;
- typedef unsigned long ODBlockSize;
-
- #if MM_DEBUG
- //========================================================================================
- // ODMemoryHook (MM_DEBUG only)
- //
- // A Memory hook that can be used to track heap operations by subclassing ODMemoryHook
- // and overriding the methods of interest. Your subclass is then registered with a
- // MemoryHeap.
- //
- //========================================================================================
-
- class ODMemoryHook
- {
- public:
- // These methods should all be abstract, but MemoryHookList instantiates a
- // ODMemoryHook to use as the head of its linked list, so here we just use empty
- // inlines. A ODMemoryHook still cannot be instantiated because the constructor
- // is protected.
-
- virtual ODBlockSize GetHeaderSize();
-
- virtual ODBlockSize AboutToAllocate(ODBlockSize size) const;
- virtual void* DidAllocate(void* blk, ODBlockSize);
- virtual const void* AboutToBlockSize(const void* blk);
- virtual void* AboutToFree(void* blk);
- virtual void AboutToRealloc(void*& , ODBlockSize&);
- virtual void* DidRealloc(void* oldBlk, void* newBlk, ODBlockSize);
- virtual void AboutToReset();
- virtual void Comment(const char*);
-
- virtual long GetType( ) const; // Returns type of hook
-
- enum{ kNoType = 0 };
-
- virtual ~ODMemoryHook();
-
- void* operator new(SIZE_T size, MMHeapLocation = kMMTempMemory);
- void operator delete(void* ptr);
-
- protected:
-
- ODMemoryHook();
-
- private:
- ODMemoryHook* fNextHook, * fPreviousHook;
-
- friend MemoryHookList;
-
- ODMemoryHook(const ODMemoryHook& blk);
- ODMemoryHook& operator=(const ODMemoryHook& blk);
- // This class shouldn't be copied.
- };
- #endif
-
-
- #if MM_DEBUG
- //========================================================================================
- // MemoryHookList (MM_DEBUG only)
- //
- // A list of memory hooks. This is a special linked list that assumes the links for
- // the list are fields of ODMemoryHook. This avoids endless recursion were we to
- // allocate nodes for the MemoryHooks here.
- //
- //========================================================================================
-
- class MemoryHookList
- {
- public:
- MemoryHookList();
-
- void Add(ODMemoryHook* aMemoryHook);
- void Remove(ODMemoryHook* aMemoryHook);
- ODMemoryHook* First() const;
- ODMemoryHook* After( ODMemoryHook* ) const;
- ODMemoryHook* Before( ODMemoryHook* ) const;
- ODMemoryHook* Last() const;
-
- ~MemoryHookList();
-
- private:
- ODMemoryHook fHead;
-
- MemoryHookList(const MemoryHookList& blk);
- MemoryHookList& operator=(const MemoryHookList& blk);
- // This class shouldn't be copied.
-
- void* operator new( size_t ) {return 0;} // Will never be allocated from the heap
- void operator delete( void* ) { }
- };
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // HeapWalkProc (proc-ptr type)
- //----------------------------------------------------------------------------------------
-
- typedef Boolean (*HeapWalkProc)( const void *blk, unsigned long size, Boolean isObject,
- void *refCon );
-
-
- class StackCrawl; // For GetBlockStackCrawl
-
-
- //========================================================================================
- // MemoryHeap
- //
- // Abstract base class for memory heaps.
- //
- //========================================================================================
-
- class MemoryHeap
- {
- public:
- enum { kBlockTypeId = 0 };
- enum { kMagicNumber = 0x8765FEDC };
-
- // Keep a list of all heaps created. Very useful for debugging.
-
- static MemoryHeap* fHeapList;
- static MemoryHeap* GetFirstHeap();
-
- void* Allocate(ODBlockSize size);
- ODBlockSize BlockSize(const void* block) const;
- virtual unsigned long BytesAllocated() const;
- virtual unsigned long BytesFree() const = 0;
- virtual unsigned long LargestFreeBlock() const;
-
- void Free(void*);
- virtual MemoryHeap* GetNextHeap() const;
- virtual Boolean GetZapOnAllocate() const;
- virtual Boolean GetZapOnFree() const;
- virtual unsigned long HeapSize() const = 0;
- virtual unsigned long NumberAllocatedBlocks() const;
- void Reset();
- void* Reallocate(void* block, ODBlockSize newSize);
- virtual void SetZapOnAllocate(Boolean = false);
- virtual void SetZapOnFree(Boolean = false);
-
- inline MMHeapLocation GetLocation( ) {return fMemSource;}
-
- void* operator new(SIZE_T size, MMHeapLocation =kMMTempMemory);
- void operator delete(void* ptr);
-
- virtual ~MemoryHeap();
-
- static const char* kDefaultDescription;
- static const char* kDeadHeapDescription;
- virtual const char* GetDescription() const;
- virtual void SetDescription(const char* description = kDefaultDescription);
-
- // Access to the isObject flag of a block. The intended use is that 'operator new'
- // will set the flag for all SOM objects, giving us a way to tell whether any block
- // is an object. This can help a lot in debugging. --jpa
- void SetBlockIsObject( void* blk, Boolean isObject );
- Boolean BlockIsObject( const void* blk ) const;
-
- MemoryHeap* GetBlockHeap( const void* ) const; // Should be static but can't be made so
-
- MMBoolean AllocateSlushFund( size_t size, size_t allocSizeLimit );
- size_t GetSlushFundSize( ) {return fSlushFundSize;}
- size_t FreeSlushFund( ); // returns size freed
-
- #if MM_DEBUG
- virtual void AdoptHook(ODMemoryHook* ODMemoryHook);
- virtual void DeleteHook(ODMemoryHook* ODMemoryHook);
-
- virtual Boolean GetAutoValidation() const;
- virtual void SetAutoValidation(Boolean = false);
- Boolean IsValidBlock(const void* blk) const;
- virtual Boolean IsMyBlock(const void* blk) const = 0;
- MMBoolean FindBlockContaining( const void *start, const void *end,
- const void* &blockStart, const void* &blockEnd ) const;
-
- virtual void Check( HeapWalkProc proc =NULL, void *refCon =NULL ) const = 0;
- virtual void Print(char* msg = "") const = 0;
-
- // Access to block StackCrawl.
- void SetBlockStackCrawl( const void* blk, StackCrawl* );
- StackCrawl* GetBlockStackCrawl( const void* blk ) const;
- #endif
-
- MemoryHeap(Boolean autoValidation = false,
- Boolean zapOnAllocate = true,
- Boolean zapOnFree = false,
- MMHeapLocation =kMMTempMemory);
-
- virtual void* AllocateRawMemory(ODBlockSize size);
- virtual void* DoAllocate(ODBlockSize size, ODBlockSize& allocatedSize) = 0;
- virtual ODBlockSize DoBlockSize(const void* block) const = 0;
- virtual void DoFree(void*) = 0;
- virtual void DoReset() = 0;
- virtual void* DoReallocate(void* block, ODBlockSize newSize, ODBlockSize& allocatedSize);
- virtual void FreeRawMemory(void* ptr);
- virtual unsigned long DoLargestFreeBlock() const = 0;
-
- virtual void DoSetBlockIsObject( void* ptr, Boolean isObject ) = 0;
- virtual Boolean DoBlockIsObject( const void* ptr ) const = 0;
- virtual MemoryHeap* DoGetBlockHeap( const void* ) const = 0;
-
- #if MM_DEBUG
- virtual Boolean DoIsValidBlock(const void* blk) const = 0;
- virtual MMBoolean DoFindBlockContaining( const void *start, const void *end,
- const void* &blockStart, const void* &blockEnd ) const = 0;
- virtual void CompilerCheck();
- #endif
-
- #if MM_DEBUG
- MMBoolean ValidateMagicNumber( ) const;
- #else
- inline void ValidateMagicNumber( ) const{ }
- #endif
-
- private:
- MemoryHeap* fNextHeap;
- MMHeapLocation fMemSource;
- Boolean fZapOnAllocate;
- Boolean fZapOnFree;
- Boolean fAutoValidation;
- unsigned long fBytesAllocated;
- unsigned long fNumberAllocatedBlocks;
- long fMagicNumber;
- MMBlock fSlushFund;
- size_t fSlushFundSize;
- size_t fSlushFundAllocSizeLimit; // Max request size to use slush fund for
-
- enum { kDescriptionLength = 64 };
- char fDescription[kDescriptionLength];
-
- #if MM_DEBUG
- MemoryHookList fMemoryHookList;
-
- protected:
- ODBlockSize CallGetHeaderSize() const;
-
- ODBlockSize CallAboutToAllocateHooks(ODBlockSize size) const;
- void* CallDidAllocateHooks(void* blk, ODBlockSize size);
- const void* CallAboutToBlockSizeHooks(const void* blk) const;
- void* CallAboutToFreeHooks(void* blk);
- void CallAboutToReallocHooks(void*& blk, ODBlockSize& size);
- void* CallDidReallocHooks(void* oldBlk, void* blk, ODBlockSize size);
- void CallAboutToResetHooks();
- void CallCommentHooks(const char* comment);
- void ValidateAndReport(void* blk) const;
- #endif
-
- private:
- MemoryHeap(const MemoryHeap& blk);
- MemoryHeap& operator=(const MemoryHeap& blk);
- // This class shouldn't be copied.
- };
-
-
- #endif
-